home *** CD-ROM | disk | FTP | other *** search
- VERSION 4.00
- Begin VB.Form BinTreeForm
- Caption = "Binary Tree"
- ClientHeight = 4140
- ClientLeft = 2445
- ClientTop = 1620
- ClientWidth = 4470
- Height = 4830
- Left = 2385
- LinkTopic = "Form1"
- ScaleHeight = 276
- ScaleMode = 3 'Pixel
- ScaleWidth = 298
- Top = 990
- Width = 4590
- Begin VB.PictureBox Canvas
- AutoRedraw = -1 'True
- Height = 3495
- Left = 0
- ScaleHeight = 229
- ScaleMode = 3 'Pixel
- ScaleWidth = 293
- TabIndex = 3
- Top = 0
- Width = 4455
- End
- Begin VB.CommandButton CmdGo
- Caption = "Go"
- Default = -1 'True
- Height = 495
- Left = 1920
- TabIndex = 2
- Top = 3600
- Width = 735
- End
- Begin VB.TextBox LevelText
- Height = 285
- Left = 480
- MaxLength = 3
- TabIndex = 1
- Text = "5"
- Top = 3720
- Width = 375
- End
- Begin VB.Label Label1
- Caption = "Level"
- Height = 255
- Left = 0
- TabIndex = 0
- Top = 3720
- Width = 495
- End
- Begin VB.Menu mnuFile
- Caption = "&File"
- Begin VB.Menu mnuFileExit
- Caption = "E&xit"
- End
- End
- Attribute VB_Name = "BinTreeForm"
- Attribute VB_Creatable = False
- Attribute VB_Exposed = False
- Option Explicit
- Const PI = 3.14159
- Const PI_2 = PI / 2
- Const PI_5 = PI / 5
- Const LENGTH_SCALE = 0.75
- Const DTHETA = PI_5
- Dim TheLevel As Integer
- Dim StartX As Integer
- Dim StartY As Integer
- Dim StartLength As Integer
- ' ************************************************
- ' Recursively draw a binary tree branch.
- ' ************************************************
- Sub DrawBranch(level As Integer, x As Integer, y As Integer, length As Integer, theta As Single)
- Dim x1 As Integer
- Dim y1 As Integer
- ' See where this branch should end.
- x1 = x + length * Cos(theta)
- y1 = y + length * Sin(theta)
- Canvas.Line (x, y)-(x1, y1)
- ' If level > 1, draw the attached branches.
- If level > 1 Then
- DrawBranch level - 1, x1, y1, length * LENGTH_SCALE, theta + DTHETA
- DrawBranch level - 1, x1, y1, length * LENGTH_SCALE, theta - DTHETA
- End If
- End Sub
- Private Sub CmdGo_Click()
- Canvas.Cls
- MousePointer = vbHourglass
- DoEvents
- StartLength = (Canvas.ScaleHeight - 10) / _
- ((1 - LENGTH_SCALE ^ (TheLevel + 1)) / (1 - LENGTH_SCALE))
- DrawBranch TheLevel, StartX, StartY, StartLength, -PI_2
- MousePointer = vbDefault
- End Sub
- Private Sub Form_Load()
- TheLevel = CInt(LevelText.Text)
- End Sub
- Private Sub Form_Resize()
- Const GAP = 3
- CmdGo.Move (ScaleWidth - CmdGo.Width) / 2, _
- ScaleHeight - CmdGo.Height - GAP
- Label1.Top = CmdGo.Top + (CmdGo.Height - Label1.Height) / 2
- LevelText.Top = Label1.Top
- Canvas.Move 0, 0, ScaleWidth, CmdGo.Top - GAP
- StartX = Canvas.ScaleWidth \ 2
- StartY = Canvas.ScaleHeight - 5
- End Sub
- Private Sub LevelText_Change()
- If IsNumeric(LevelText.Text) Then
- TheLevel = CInt(LevelText.Text)
- Else
- TheLevel = -1
- End If
- CmdGo.Enabled = TheLevel > 0
- End Sub
- Private Sub mnuFileExit_Click()
- Unload Me
- End Sub
-